package com.prp.db.daoimpl.mysql;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.bo.SNSConstants;
import com.prp.db.idao.INeighbourRelationshipDAO;
import com.prp.db.po.NeighbourRelationship;
import com.prp.db.po.NeighbourRelationshipId;
import com.prp.db.po.UserInformation;

/**
 * A data access object (DAO) providing persistence and search support for
 * NeighbourRelationship entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.NeighbourRelationship
 * @author MyEclipse Persistence Tools
 */

public class NeighbourRelationshipDAO extends BaseHibernateDAO implements
		INeighbourRelationshipDAO {
	public static final String FROMID = SNSConstants.NeighbourRelationship_FromId;
	public static final String TOID = SNSConstants.NeighbourRelationship_ToId;
	public static final String BUILDUP = SNSConstants.NeighbourRelationship_BuildUp;

	private static final Log log = LogFactory
			.getLog(NeighbourRelationshipDAO.class);
	// property constants

	public void save(NeighbourRelationship transientInstance) {
		log.debug("saving NeighbourRelationship instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(NeighbourRelationship persistentInstance) {
		log.debug("deleting NeighbourRelationship instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public NeighbourRelationship findById(NeighbourRelationshipId id) {
		log.debug("getting NeighbourRelationship instance with id: " + id);
		try {
			NeighbourRelationship instance = (NeighbourRelationship) getSession()
					.get(NeighbourRelationship.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(NeighbourRelationship instance) {
		log.debug("finding NeighbourRelationship instance by example");
		try {
			List results = getSession().createCriteria(
					NeighbourRelationship.class).add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding NeighbourRelationship instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from NeighbourRelationship as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProperties(String[] propertyNames, Object[] values) {
		log.debug("finding NeighbourRelationship instance with properties: ");
		if (propertyNames.length != values.length) {
			log.debug("length not equal");
			throw new IllegalArgumentException("length not equal");
		}
		try {
			String queryString = "from NeighbourRelationship as model";
			String condition = "";
			for (int i = 0; i < propertyNames.length; i++)
				condition += "and model." + propertyNames[i] + "= ?";
			if (condition.length() != 0)
				queryString += " where " + condition.substring(3);
			Query queryObject = getSession().createQuery(queryString);
			for (int i = 0; i < values.length; i++)
				queryObject.setParameter(i, values[i]);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(int value) {
		List userinfoList = new ArrayList();
		UserInformationDAO userDAO = new UserInformationDAO();
		List list = findByProperty(SNSConstants.NeighbourRelationship_FromId,
				value);
		for (int i = 0; i < list.size(); i++) {
			NeighbourRelationship relation = (NeighbourRelationship) list
					.get(i);
			int id = relation.getId().getToId();
			UserInformation userinfo = userDAO.findById(id);
			if (userinfo != null)
				userinfoList.add(userinfo);
		}
		list = findByProperty(SNSConstants.NeighbourRelationship_ToId, value);
		for (int i = 0; i < list.size(); i++) {
			NeighbourRelationship relation = (NeighbourRelationship) list
					.get(i);
			int id = relation.getId().getToId();
			UserInformation userinfo = userDAO.findById(id);
			if (userinfo != null)
				userinfoList.add(userinfo);
		}
		return userinfoList;
	}

	public List findByBuildup(Object buildup) {
		return findByProperty(BUILDUP, buildup);
	}

	public List findAll() {
		log.debug("finding all NeighbourRelationship instances");
		try {
			String queryString = "from NeighbourRelationship";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public NeighbourRelationship merge(NeighbourRelationship detachedInstance) {
		log.debug("merging NeighbourRelationship instance");
		try {
			NeighbourRelationship result = (NeighbourRelationship) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(NeighbourRelationship instance) {
		log.debug("attaching dirty NeighbourRelationship instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(NeighbourRelationship instance) {
		log.debug("attaching clean NeighbourRelationship instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public boolean isNeighbour(int userId1, int userId2) {
		NeighbourRelationship relation = findById(new NeighbourRelationshipId(userId1, userId2));
		if (relation == null)
			relation = findById(new NeighbourRelationshipId(userId2, userId1));
		if (relation == null)
			return false;
		return relation.getBuildup();
	}

	// public static void main(String[] args){
	// NeighbourRelationshipDAO dao = new NeighbourRelationshipDAO();
	// System.out.println(dao.isNeighbour(1, 29));
	// System.out.println(dao.isNeighbour(1, 59));
	// }
}